<?php
/**
 * vgoshop
 * ============================================================================
 * 网站地址: http://www.vgo.me
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: myhonour
 * DateTime: 2017/10/10 上午6:50
 */

namespace app\admin\model;


class Themes
{
    protected static  $theme_path  =   ROOT_PATH.'themes'.DS;

    public static $default  =   'default';
    /** 获取主题 内 所有文件
     * @param $path
     * @return array
     */
    public static  function fileList($path){
        $fileList =    [];
        if(!$pathRes    =   @opendir($path)){
            return $fileList;
        };
        while (($file = readdir($pathRes)) !== FALSE){
            if ($file != '.' && $file != '..' && is_dir($path.$file)){
                $fileList = array_merge($fileList,self::fileList($path.$file.DS));
            }
            if ($file != '.' && $file != '..' && is_file($path.$file) && (substr(strrchr($file,'.'),1) == 'html')){
                $fileList[]  =   ['name' =>  $file,'path' => $path.$file,'sortPath' =>  str_replace(ROOT_PATH,'',$path.$file)];
            }
        }
        return $fileList;
    }

    public static function defaultThemes(){
        $theme_list = self::themeList(self::$theme_path);
        $theme = in_array(config('SITE_DEFAULT_THEMES'),$theme_list)?config('SITE_DEFAULT_THEMES'):self::$default;
        $listPath       =   self::$theme_path.$theme.DS.'index'.DS.'lists'.DS;
        $contentPath    =   self::$theme_path.$theme.DS.'index'.DS.'contents'.DS;
        $temp   =   [];
        $temp['list']   =   self::fileList($listPath);
        $temp['content']=   self::fileList($contentPath);
        return $temp;
    }

    /** 获取主题文件夹
     * @param $path
     * @return array
     */
    public static function themeList($path){
        $dir    =   [];
        if(!$pathRes    =   @opendir($path)){
            return $dir;
        };
        while (($file = readdir($pathRes)) !== FALSE){
            if ($file != '.' && $file != '..' && is_dir($path.$file)){
                $dir[]  =   ['name' =>  $file,'path' => $path.$file];
            }
        }
        return $dir;
    }
}